/*
 * Group:PrePool K9-357 
 * 8/2010
 */
package portlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;

import util.ConfigUtil;
import util.Constants;
import corewa.CoreWa;
import event.Event;
import form.BaseForm;
import form.Contribute;
import form.S09Form;

/**
 * OnlineDictPortlet Portlet Class
 * 
 * @author disable
 */
public class OnlineDictPortlet extends GenericPortlet {
	CoreWa coreWebAppCfg = null;
	String nextScr = null;
	BaseForm result = null;
	String contextPath = null;
	Logger LOG = Logger.getLogger("portlet.OnlineDictPortlet");
	
	@Override
	public void init(PortletConfig portletCfg) throws PortletException {
		// TODO Auto-generated method stub
		super.init(portletCfg);
		contextPath = portletCfg.getInitParameter("contextPath");
		String mvcPortletCfg = portletCfg.getInitParameter("OnlineDictConfig");
		String realPathCfg = portletCfg.getPortletContext().getRealPath(
				mvcPortletCfg);
		coreWebAppCfg = ConfigUtil.parse(realPathCfg);
	}

	public void doView(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");
		PortletRequestDispatcher dispatcher = null;
		request.getPortletSession().setAttribute("messError", "",
				PortletSession.APPLICATION_SCOPE);

		if (nextScr == null) {
			request.getPortletSession().setAttribute("username", "Guest",
					PortletSession.APPLICATION_SCOPE);
			request.getPortletSession().setAttribute("userrole", -1,
					PortletSession.APPLICATION_SCOPE);
			dispatcher = getPortletContext().getRequestDispatcher(
					"/WEB-INF/jsp/S01_LookUpWord.jsp");
		} else {
			if (result instanceof form.ManageHome) {
				form.ManageHome managerForm = (form.ManageHome) result;
				String username = managerForm.getLoginUsername();
//				LOG.info(username);
				int role = managerForm.getRole();
//				LOG.info(role);
				if (role == -1) {
					// login fail
					request.getPortletSession().setAttribute("messError","Invalid user and password",PortletSession.APPLICATION_SCOPE);
					nextScr = "/WEB-INF/jsp/S03_Login.jsp";
					request.setAttribute("result", managerForm);
				}
				if (role == Constants.ROLE_USER) {
					// user
					request.getPortletSession().setAttribute("username",
							username, PortletSession.APPLICATION_SCOPE);
					request.getPortletSession().setAttribute("userrole", role,
							PortletSession.APPLICATION_SCOPE);
					nextScr = "/WEB-INF/jsp/S01_LookUpWord.jsp";
					request.setAttribute("result", managerForm);
				}
				if (role == Constants.ROLE_DICT_MANAGER) {
//					LOG.info(nextScr);
					// manager
					request.getPortletSession().setAttribute("username",
							username, PortletSession.APPLICATION_SCOPE);
					request.getPortletSession().setAttribute("userrole", role,
							PortletSession.APPLICATION_SCOPE);
				// I NEED MY BEAN !
					request.setAttribute("result", managerForm);
				}
			}
			if (result instanceof form.LogoutForm) {
				request.getPortletSession().setAttribute("userrole", -1,
						PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().setAttribute("username", "Guest",
						PortletSession.APPLICATION_SCOPE);
				nextScr = "/WEB-INF/jsp/S01_LookUpWord.jsp";
			}
			if (result instanceof form.SearchWord) {
				request.setAttribute("result", result);
			}
			if (result instanceof form.Contribute) {
				form.Contribute contribute = (Contribute) result;
				if (contribute.getSuccess() == 1) {
					request
							.getPortletSession()
							.setAttribute(
									"message",
									"Thank you for your contribute."
											+ "<br>The word you suggest is waiting for approving."
											+ "<br>We will email you as soon as.",
									PortletSession.APPLICATION_SCOPE);
				} else {
					request.getPortletSession().setAttribute(
							"message",
							"We can not insert your suggested word"
									+ "<br>It may be caused error system."
									+ "<br>Please try again later.",
							PortletSession.APPLICATION_SCOPE);
				}
				request.setAttribute("result", null);
			}
			if(result instanceof form.S09Form){
				S09Form temp = (S09Form)result;
				request.setAttribute("dictID", temp.getDicID());
								
			}
			if(result instanceof form.S14Form){
				
			}
			
			
			
			
			
			
			
			// for debugging
			
			// prepare the destination
			LOG.info(nextScr);
			dispatcher = getPortletContext().getRequestDispatcher(nextScr);
		}

		request.setAttribute("contextPath", contextPath);
		dispatcher.include(request, response);

	}

	public void doEdit(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");

		PortletRequestDispatcher dispatcher = getPortletContext()
				.getRequestDispatcher("/WEB-INF/jsp/OnlineDictPortlet_edit.jsp");
		dispatcher.include(request, response);

	}

	public void doHelp(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");

		PortletRequestDispatcher dispatcher = getPortletContext()
				.getRequestDispatcher("/WEB-INF/jsp/OnlineDictPortlet_help.jsp");
		dispatcher.include(request, response);

	}

	public void processAction(ActionRequest request, ActionResponse response)
			throws PortletException, IOException {
		String screenId = request.getParameter("screenId");
		String eventId = request.getParameter("eventId");
		screen.Screen screen = coreWebAppCfg.getScreen(screenId);
		Event event = screen.getEvent(eventId);
		String procId = null;
		if (event != null) {
			procId = event.getProcId();
			nextScr = event.getNextScrId();
		}
		try {
			Method method;
			Object controlClass = Class.forName(screen.getCtrlClass())
					.newInstance();
			method = controlClass.getClass().getMethod(procId,
					ActionRequest.class, ActionResponse.class);
			result = (BaseForm) method.invoke(controlClass, request, response);
		} catch (Exception ex) {
		}

	}

}
